package main

import (
	"battle-server/config"
	"battle-server/database"
	"battle-server/server"
	"fmt"
	"log"
	"net"
	pb "proto"

	"google.golang.org/grpc"
)

func main() {
	// 加载配置
	if err := config.LoadConfig("config.yaml"); err != nil {
		panic(fmt.Sprintf("Error loading config: %v", err))
	}

	// 初始化数据库
	dbConfig := config.GlobalConfig.Database
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%v&loc=%s",
		dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port,
		dbConfig.DBName, dbConfig.Charset, dbConfig.ParseTime, dbConfig.Loc)

	if err := database.InitDB(dsn); err != nil {
		panic(fmt.Sprintf("Error connecting to database: %v", err))
	}

	lis, err := net.Listen("tcp", ":50052")
	if err != nil {
		log.Fatalf("Failed to listen: %v", err)
	}

	s := grpc.NewServer()
	battleServer := server.NewBattleServer()
	pb.RegisterBattleServiceServer(s, battleServer)

	fmt.Println("Battle server starting on :50052")
	if err := s.Serve(lis); err != nil {
		log.Fatalf("Failed to serve: %v", err)
	}
}
